﻿@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>EditCourse</title>
    <script src="~/Content/js/jquery/jquery-1.12.1.min.js"></script>
</head>
<body>
    <div>
        <div>
            <span>课程标题：</span>
            <span>
                <input type="text" name="CourseTitle" value="" />
            </span>
        </div>
        <div>
            <span>课程标签：</span>
            <span>
                <input type="text" name="CourseTag" value="" />
            </span>
        </div>
        <div>
            <span>课程价格：</span>
            <span>
                <input type="text" name="CoursePrice" value="" />
            </span>
        </div>
        <div>
            <span>课程时间：</span>
            <span>
                <input type="text" name="CourseTime" value="" />
            </span>
        </div>
        <div>
            <span>开始时间：</span>
            <span>
                <input type="text" name="StartTime" value="" />
            </span>
        </div>
        <div>
            <span>结束时间：</span>
            <span>
                <input type="text" name="EndTime" value="" />
            </span>
        </div>
        <div>
            <span>详细地址：</span>
            <span>
                <input type="text" name="DetailAddress" value="" />
            </span>
        </div>
        <div>
            <span>学生人数：</span>
            <span>
                <input type="text" name="StudentCount" value="" />
            </span>
        </div>
        <div>
            <span>描述信息：</span>
            <span>
                <textarea name="Description" rows="4" cols="30"></textarea>
            </span>
        </div>
        <div>
            <span>课程内容：</span>
            <span>
                <textarea name="Content" rows="4" cols="50"></textarea>
            </span>
        </div>
        <div>
            <input type="button" id="btn_commint" onclick="AddCourse()" value="添加课程" />
        </div>
    </div>
    <script type="text/javascript">
        function ConvertToKeyValueCollection(target) {
            var array = null;
            if (target == null || typeof (target) != "object") {
                return array;
            }
            array = [];
            var keyValue;
            for (var key in target) {
                keyValue = { Key: "", Value: "" };
                keyValue.Key = key;
                keyValue.Value = target[key];
                array.push(keyValue);
            }
            return array;
        }
        function AddCourse() {
            var objCourse = {
                CourseTitle: $("input[name=CourseTitle]").val(),
                CourseTag: $("input[name=CourseTag]").val(),
                CoursePrice: $("input[name=CoursePrice]").val(),
                CourseTime: $("input[name=CourseTime]").val(),
                StartTime: $("input[name=StartTime]").val(),
                EndTime: $("input[name=EndTime]").val(),
                DetailAddress: $("input[name=DetailAddress]").val(),
                StudentCount: $("input[name=StudentCount]").val(),
                Description: $("textarea[name=Description]").val(),
                Content: $("textarea[name=Content]").val(),
                Status: 1
            };
            if (!objCourse.CourseTitle) {
                alert("请输入课程标题");
                return;
            }
            //todo：其他验证等等

            ////mvc-post
            //$.ajax({
            //    cache: false,
            //    type: "Post",
            //    url: "/apitest/addcourse",
            //    async: false,
            //    data:
            //    {
            //        Appsettiing: "Bigger_Course",
            //        ApiGroupId: "ApiGroup_Course",
            //        ApiId: "Bigger_CourseInsertByParameters",
            //        DbParameters: objCourse
            //    },
            //    dataType: "JSON",
            //    success: function (objInvokeResult) {
            //        if (objInvokeResult.ResultCode != "Success") {
            //            //不成功做点什么
            //        }
            //        alert(objInvokeResult.ResultMessage);
            //    }
            //});

            //webapi-post
            $.ajax({
                cache: false,
                type: "Post",
                url: "/api/values",//WebApi对Api包的解析是有问题的
                async: false,
                data:
                {
                    Appsettiing: "Bigger_Course",
                    ApiGroupId: "ApiGroup_Course",
                    ApiId: "Bigger_CourseInsertByParameters",
                    DbParameters: ConvertToKeyValueCollection(objCourse)
                },
                dataType: "JSON",
                success: function (objInvokeResult) {
                    if (objInvokeResult.ResultCode != "Success") {
                        //不成功做点什么
                    }
                    alert(objInvokeResult.ResultMessage);
                }
            });
        }
    </script>
</body>
</html>
